package uk.ac.cam.echo.app;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import uk.ac.cam.echo.ScheduleEvent;
import uk.ac.cam.echo.messages.CheckInMessage;
import uk.ac.cam.echo.messages.LocationMessage;
import uk.ac.cam.echo.messages.Message;
import uk.ac.cam.echo.messages.SecurityMessage;
import uk.ac.cam.echo.messages.UserMessage;

/**
 * @author Tom Smith
 */
public class Program {

	private static String host = "localhost";
	private static int port = 44444;
		
	private static DBInt DBMainApp;
	private static DBSecurity DBSecurityApp;
	
	public static void main(String[] args) {

		new Thread() {
			public void run() {

				try {

					// Start
					DBMainApp = DBInt.getInstance("E00700000299CE84", "rob.c", host, port);;
					
					// What's the schedule?
					for (ScheduleEvent s : DBMainApp.getSchedule()) {
						System.out.println(s.toString());
					}
					
					System.out.println();
					
					// Scan an RFID tag.
					handleRFIDTag(DBMainApp.scanRFID("E00700000299BDE7"));
					
					System.out.println();

					// Get friend requests.
					System.out.println("You have the following friend requests:");
					for (UserMessage u : DBMainApp.getFriendRequests()) {
						System.out.println(u.toString());
					}
					
					System.out.println();
					
					// Get friend list.
					System.out.println("These are your friends:");
					for (UserMessage u : DBMainApp.getFriendList()) {
						System.out.println(u.toString());
					}
					
					System.out.println();
					
					// Get friend check-ins.
					System.out.println("These are your friend's latest check ins:");
					for (CheckInMessage u : DBMainApp.getFriendCheckIns()) {
						System.out.println(u.toString());
					}
					
					System.out.println();

					// Scan an RFID tag.
					handleRFIDTag(DBMainApp.scanRFID("E00700000299C27C"));
					
					
				} catch (UnknownHostException e) {
					System.err.println("Could not connect to the host because the host is unknown.");
				} catch (ConnectException e) {
					System.err.println("Could not connect to the host because the host refused the connection.");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}.start();
		
		new Thread() {
			public void run() {

				try {

					// Start
					DBSecurityApp = DBSecurity.getInstance(host, port);;
					
					// I'm a security guard.
					SecurityMessage s = DBSecurityApp.getSecurityDetails("E00700000299C0D1");
					
					System.out.println("FORENAME: " + s.getForename());
					System.out.println("SURNAME: " + s.getSurname());
					System.out.println("USER TYPE: " + s.getUserType());
					System.out.println("GUEST: " + s.getAllowed());
					System.out.println("CHECK INS:");
					
					for (CheckInMessage c : s.getCheckIns()) {
						System.out.println(c.getFormattedCheckInTime() + " " + c.getAttractionName());
					}
					
					
				} catch (UnknownHostException e) {
					System.err.println("Could not connect to the host because the host is unknown.");
				} catch (ConnectException e) {
					System.err.println("Could not connect to the host because the host refused the connection.");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}.start();
	}
	
	private static void handleRFIDTag(Message m) throws IOException, InterruptedException {
		
		if (m instanceof UserMessage) {
			System.out.println("You just scanned " + ((UserMessage)m).getForename() +  " " + ((UserMessage)m).getSurname() + "'s wristband. " +
					"Add as friend?");
			
			// Adds even if the friend has already been added, however no negative effect.
			DBMainApp.addFriend(m.getRFID());
			
		} else if (m instanceof LocationMessage) {

			System.out.println("Schedule for: " + ((LocationMessage)m).getLocationName());
			
			for (ScheduleEvent s : DBMainApp.getSchedule(m.getRFID())) {
				System.out.println(s.toString());
			}
			
			// Open map for m.getRFID()
			
		} else if (m instanceof SecurityMessage) {
			// User should not get this ever.

		} else {
			// unknown.
		}
		
	}
}
